// 新增研发任务组件

// 新增研发饱和度指标组件


// 新增团队情况组件


import React, { Component } from 'react';
import NetUitl from '../utils/HttpUtil';
import {Input,Table,Button,Modal ,message} from "antd";
import TitleBar from "./TitleBar";
import moment from 'moment';
import "./add.scss";


let checkInt =(val)=>{
    let istrue = true
    var re =  /^(0|\+?[1-9][0-9]*)$/;
    if (val !== ""){
        if (!re.test(val)) {
            istrue=false
             message.warning("请输入正整数！");
        }
    }
    return istrue
}

class AddTask extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataSource:[
                // {
                //     module:"",
                //     progress:"",
                //     content:"",
                //     project:"",
                //     openProblemNum:"",
                //     problemProjectNum:"",
                //     supportPeopleNum:""
                // }
            ],
            selectedRowKeys: [],
            team:1,
            id:1,
            iscopy:false
        }
        this.handleCopy = this.handleCopy.bind(this)
        this.columns1=[
            {
                title: '产品线模块',
                dataIndex: 'module',
                width:"19%",
                render: (text,index,record)=>{
                    console.log(record,"recodr")
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.module} onChange={(e)=>this.moduleChange(record,index,e)}></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '研发进度',
                dataIndex: 'progress',
                width:"12%",
                render: (text,index,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.progress}  onChange={(e)=>this.rateChange(record,index,e)} ></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '当日工作',
                dataIndex: 'content',
                width:"69%",
                render: (text,index,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.content}  onChange={(e)=>this.workChange(record,index,e)} ></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            }
        ]

        this.columns2=[
            {
                title: '区域-项目',
                dataIndex: 'project',
                // width:"23%",
                render: (text,index,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.project}  onChange={(e)=>this.proChange(record,index,e)} ></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '未解决问题',
                dataIndex: 'openProblemNum',
                width:"30%",
                render: (text,index,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.openProblemNum}  onChange={(e)=>this.openChange(record,index,e)} ></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '问题分布项目数',
                dataIndex: 'problemProjectNum',
                width:"20%",
                render: (text,index,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.problemProjectNum}  onChange={(e)=>this.problemChange(record,index,e)} ></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            },
            {
                title: '支持人员数',
                dataIndex: 'supportPeopleNum',
                width:"20%",
                render: (text,index,record)=>{
                    if (this.props.isAdd === true){
                        return <Input placeholder={"请填写"} value={index.supportPeopleNum}   onChange={(e)=>this.supportChange(record,index,e)} ></Input>
                    }else{
                        return <span>{text}</span>
                    }
                }
            }
        ]

    }

    componentDidMount(){
        this.publicFn(this.props)
    }
    componentWillReceiveProps(nextprops){
        this.publicFn(nextprops)
    }

    //接收父组件传的值
    publicFn(nextprops){
        let task =nextprops.allData &&  nextprops.allData.task?nextprops.allData.task:""
        let {id,date, stateType} = nextprops
        let dataSource= task;
        console.log(nextprops,"componentWillReceiveProps")
        // if (stateType==1){
        //     this.setState({dataSource:[],id:id,date:date,stateType:stateType})
        // }else{
        //     this.setState({dataSource:dataSource,id:id,date:date,stateType:stateType})
        // }

        this.setState({dataSource:dataSource,id:id,date:date,stateType:stateType})

    }


    //产品线模块change事件
    moduleChange=(record,index,e)=>{
        let {dataSource} = this.state;
        if(!!e.target.value && e.target.value.length>20){
            return message.warning('文字长度不能超过20个字节！')
        }
        dataSource[record].module= e.target.value;
        console.log(dataSource,"dd ",index,record,e.target.value)
        this.setState({dataSource});

        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    // 研发进度  change事件
    rateChange=(record,index,e)=>{
        let {dataSource} = this.state;
        if(!!e.target.value && e.target.value.length>20){
            return message.warning('文字长度不能超过10个汉字！')
        }
        dataSource[record].progress = e.target.value;
        this.setState({dataSource});

        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    // 当日工作  change事件
    workChange=(record,index,e)=>{
        let {dataSource} = this.state;
        if(!!e.target.value && e.target.value.length>500){
            return message.warning('文字长度不能超过255个汉字！')
        }
        dataSource[record].content = e.target.value;
        this.setState({dataSource});

        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    // 区域-项目  change事件
    proChange =(record,index,e)=>{
        let {dataSource} = this.state;
        dataSource[record].project = e.target.value;
        console.log(record, e.target.value," e.target.value",index)
        this.setState({dataSource});

        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    // 未解决问题  change事件
    openChange  =(record,index,e)=>{
        // var re =  /^(0|\+?[1-9][0-9]*)$/;
        // if (e.target.value !== ""){
        //     if (!re.test( e.target.value)) {
        //         return  message.warning("请输入正整数！");
        //     }
        // }
        if(checkInt(e.target.value)){
            let {dataSource} = this.state;
            dataSource[record].openProblemNum = e.target.value;
            this.setState({dataSource});
        }

        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    // 问题分布项目数 change事件
    problemChange  =(record,index,e)=>{

        // var re =  /^(0|\+?[1-9][0-9]*)$/;
        // if (e.target.value !== ""){
        //     if (!re.test( e.target.value)) {
        //         return  message.warning("请输入正整数！");
        //     }
        // }
        if(checkInt(e.target.value)){
            let {dataSource} = this.state;
            dataSource[record].problemProjectNum = e.target.value;
            this.setState({dataSource});
        }


        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    // 支持人员数  change事件
    supportChange =(record,index,e)=>{

        // var re =  /^(0|\+?[1-9][0-9]*)$/;
        // if (e.target.value !== ""){
        //     if (!re.test( e.target.value)) {
        //         return  message.warning("请输入正整数！");
        //     }
        // }
        if(checkInt(e.target.value)){
            let {dataSource} = this.state;
            dataSource[record].supportPeopleNum = e.target.value;
            this.setState({dataSource});
        }
        // let {dataSource} = this.state;
        // dataSource[record].supportPeopleNum = e.target.value;
        // this.setState({dataSource});

        // let allData={};
        // allData.task = dataSource[0];
        // this.props.viewer.setState({allData})
    }

    //选择行数
    onSelectChange = selectedRowKeys => {
        console.log('selectedRowKeys changed: ', selectedRowKeys);
        this.setState({ selectedRowKeys });
    };

    //增加新行数
    addTaskRow = ()=>{
        let {dataSource} = this.state;
        let newData ={};
        let teamId = this.props.allData.team.teamId;
        let obj = Object.keys(dataSource)
        if(obj.length>=12){
            message.warning('研发任务不能超过12条！')
        }
        if ( teamId === 1){
             newData = {
                 module:"",
                 progress:"",
                 content:""
            }
        }else{
            newData = {
                project:"",
                openProblemNum:"",
                problemProjectNum:"",
                supportPeopleNum:""
            }
        }

        dataSource.push(newData);
        this.setState({dataSource});
    }

    //删除行数
    deteTaskRow = ()=>{
        let {selectedRowKeys,dataSource} =this.state;
        console.log(selectedRowKeys)
        let _this=this;
        if (selectedRowKeys.length !== 0) {
            Modal.confirm({
                iconType: "exclamation-circle",
                title: '确定删除选中数据吗?',
                okText: '确认',
                cancelText: '取消',
                centered:true,
                onOk() {
                    for (let index of selectedRowKeys){
                        dataSource.splice(index,1,undefined);
                    }
                    console.log(dataSource)
                    for(var i = 0; i <  dataSource.length; i++) {
                        if( dataSource[i] === undefined) {
                            dataSource.splice(i,1);
                            i = i - 1; // i - 1 ,因为空元素在数组下标 2 位置，删除空之后，后面的元素要向前补位，
                            // 这样才能真正去掉空元素,觉得这句可以删掉的连续为空试试，然后思考其中逻辑
                        }
                    }
                    console.log(dataSource)
                    _this.setState({dataSource,selectedRowKeys:[]});
                    console.log(dataSource)
                    message.success('删除成功！');
                },
                onCancel() {
                },
            });
        } else {
            message.warning('请选择需要删除的数据');
        }
    }

    //拷贝事件
    handleCopy(){
        // let id = 
        let {id,date} = this.props;
        let {iscopy} = this.state;
        if(!iscopy){
            let day = new Date(date)
            let newDate = `${day.getFullYear()}-${day.getMonth()+1}-${day.getDate()-1}`; 
                newDate = moment(newDate).format('YYYY-MM-DD')
            let url = `dailyReport/getReportDetail?teamId=${id}&date=${newDate}`;
            let self = this;
            console.log("handleCopy",id,this.state,this.props)
            NetUitl.get(url, null ,function (data) {
                if (data && data.msgCode == 200) {
                    console.log(22222222222,data.data)
                    if(data.data.task && data.data.task.length){
                        self.setState({dataSource:data.data.task,iscopy:true})
    
                    }else{
                        self.setState({iscopy:true})
                        message.warning('昨日内容为空！');
                    }
                    // self.setState({allData:allData})
                }else{
                    message.error("后台数据问题");
                }
    
            }, function (data) {
                message.error("后台数据问题");
            })
        }
    }

    // saveTable = () =>{
    //     const {allData}
    // }

    render() {
        const isAdd = this.props.isAdd;
        const { selectedRowKeys,team ,id,stateType,date,iscopy} = this.state;
        const rowSelection = {
            selectedRowKeys,
            onChange: this.onSelectChange,
            columnWidth:40
        };
        const teamId = id;
        console.log(this.state.dataSource,id)

        return (
            <div className="addTask-box">
                <div className={"addTask-top"}>
                    <div>
                        <TitleBar name={"研发任务"}></TitleBar>
                    </div>
                    {isAdd === true ?
                        <div>
                            {
                                stateType && stateType==1?
                                <Button onClick={this.handleCopy} style={{marginRight:8}} disabled={iscopy}>拷贝昨天数据</Button>
                                :null
                            }
                            <Button style={{marginRight:8}} onClick={this.deteTaskRow}>删除</Button>
                          
                            <Button type={"primary"} onClick={this.addTaskRow}>新增</Button>

                        </div>  :  ""
                    }
                </div>
                <div className={"addTask-main"}>
                    <Table
                        className={"addTask-table"}
                        bordered
                        rowSelection={ isAdd === false ? null : rowSelection}
                        columns={id == "3" ? this.columns2 : this.columns1}
                        dataSource={this.state.dataSource}
                        pagination={false}
                    />
                </div>
            </div>
        );
    }
}

export default  AddTask;
